package com.engine4cloud.tdf.supervision.controller.web;

import cn.hutool.core.collection.CollUtil;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.log.annotation.SysLog;
import com.engine4cloud.tdf.supervision.dto.create.RcglCommunityServicesCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.RcglCommunityServicesQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.RcglCommunityServicesResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.RcglCommunityServicesUpdateDTO;
import com.engine4cloud.tdf.supervision.service.RcglCommunityServicesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 社区服务-web
 *
 * @author wenjie
 * @date 2024-04-26 14:43:42
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/web/community/services")
@Tag(description = "CommunityServices", name = "日常管理-社区服务管理-web")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RcglCommunityServicesWebController {

    private final RcglCommunityServicesService communityServicesService;

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @param dto  社区服务
     * @return R<PageInfo < RcglCommunityServicesResultDTO>>
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R<PageInfo<RcglCommunityServicesResultDTO>> getPageList(@ParameterObject Params page, @Valid @ParameterObject RcglCommunityServicesQueryDTO dto) {
        return R.ok(communityServicesService.getPageList(page, dto));
    }


    /**
     * 通过id查询社区服务
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R<RcglCommunityServicesResultDTO> getById(@PathVariable("id") Long id) {
        return R.ok(communityServicesService.getById(id));
    }

    /**
     * 新增社区服务
     *
     * @param dto 社区服务
     * @return R
     */
    @Operation(summary = "新增社区服务", description = "新增社区服务")
    @SysLog("新增社区服务")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('community_services_add')")
    public R<Boolean> create(@Valid @RequestBody RcglCommunityServicesCreateDTO dto) {
        return R.ok(communityServicesService.create(dto));
    }

    /**
     * 修改社区服务
     *
     * @param dto 社区服务
     * @return R
     */
    @Operation(summary = "修改社区服务", description = "修改社区服务")
    @SysLog("修改社区服务")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('community_services_edit')")
    public R<Boolean> update(@Valid @RequestBody RcglCommunityServicesUpdateDTO dto) {
        return R.ok(communityServicesService.update(dto));
    }

    /**
     * 通过id删除社区服务
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除社区服务", description = "通过id删除社区服务")
    @SysLog("通过id删除社区服务")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('community_services_del')")
    public R<Boolean> removeById(@RequestBody Long[] ids) {
        return R.ok(communityServicesService.removeBatchByIds(CollUtil.toList(ids)));
    }
}